
from pygame import Rect, display

# Convert a point to a pygame Rect
def rectify(p, s=(0,0)): return Rect(p[0], p[1], s[0], s[1])

# Convert a string into x, y coordinates
def coordify(s): return tuple(int(i.strip()) for i in s.split(','))
            
# Normalizes the indentation of a block of text
def indentify(text):
    lines = text.split("\n")
    for i, x in enumerate(lines): 
        if not x or x.isspace(): del lines[i]
        tab = len(lines[0]) - len(lines[0].lstrip())
    return "\n".join(i[tab:] for i in lines)

# Creates dict from a block of line delimited text
def text2dict(text, s1=";", s2=":"):
    r = {}
    for i in text.split(s1): 
        if s2 in i: 
            v = i.split(s2, 1)
            if len(v) == 2: 
                v = [i.strip() for i in v]
                if v[1].isdigit(): v[1] = int(v[1])
                r[v[0].strip()] = v[1]
    return r

# Verifies that an object is of a certain class and returns it
def type_valid(obj, cls):
    assert isinstance(obj, cls), str(obj) + " is required to be of type " + str(cls)
    return obj
    
sequence = (tuple, list)
    
    
# Create an object with its __dict__ set to a custom definition
class Object(object):
    
    def __new__(cls, dict):
        cls = object.__new__(cls)
        cls.__dict__.update(dict)
        return cls
    
